package com.czl.module_base.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.viewbinding.ViewBinding;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.databinding.FragmentBaseRecyclerViewBinding;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

/**
 * @author by xubin,
 * @date on 2022/1/26.
 * PS: Not easy to write code, please indicate.
 */
public abstract class BaseRecyclerViewFragment<V extends ViewBinding> extends BaseFragment<V> {

    protected FragmentBaseRecyclerViewBinding mRecyclerViewBinding;
    protected boolean hasData;

    @Override
    protected View initView(LayoutInflater layoutInflater, ViewGroup container) {
        mRecyclerViewBinding = FragmentBaseRecyclerViewBinding.inflate(layoutInflater, container, false);
        binding = getBinding(getLayoutInflater(), mRecyclerViewBinding.containerTop);
        return mRecyclerViewBinding.getRoot();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mRecyclerViewBinding.smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            refreshLayout.setEnableLoadMore(true);
            reload();
        });
        mRecyclerViewBinding.smartRefreshLayout.setOnLoadMoreListener(refreshLayout -> loadMore());
    }

    protected void loadMore() {

    }

    @Override
    public void showLoading() {
        if (!hasData) {
            super.showLoading();
        }
    }

    protected void setEmptyView(BaseQuickAdapter<?, ?> adapter) {
        adapter.removeEmptyView();
        View emptyView = View.inflate(requireContext(), com.czl.module_base.R.layout.common_empty_layout, null);
        emptyView.findViewById(com.czl.module_base.R.id.ll_empty).setOnClickListener(v -> mRecyclerViewBinding.smartRefreshLayout.autoRefresh());
        adapter.setEmptyView(emptyView);
    }

    protected <T> void handleResponseData(BaseQuickAdapter<T, ? extends BaseViewHolder> adapter,
                                          int page, ListBean<T> list) {

        if (mRecyclerViewBinding.recyclerView.getAdapter() == null) {
            mRecyclerViewBinding.recyclerView.setAdapter(adapter);
        }
        SmartRefreshLayout refreshLayout = mRecyclerViewBinding.smartRefreshLayout;
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        showLoadSuccess();
        if (page == 1) {
            if (list == null || list.getList() == null || list.getList().isEmpty()) {
                hasData = false;
                setEmptyView(adapter);
                refreshLayout.setEnableLoadMore(false);
                return;
            }
            hasData = true;
            adapter.setList(list.getList());
        } else {
            hasData = true;
            if (list != null && list.getList() != null) {
                adapter.addData(list.getList());
            }
        }
        if (list == null || list.getTotal() == list.getList().size() || !list.hasNextPage()) {
            refreshLayout.finishLoadMoreWithNoMoreData();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mRecyclerViewBinding != null) {
            mRecyclerViewBinding.unbind();
        }
    }
}
